﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Base64
{
    /// <summary>
    /// Decode the base64 text and create the file within the decoding.
    /// </summary>
    /// <datetime>04/10/2012-10:56</datetime>
    class Decode64
    {
        /// <summary>
        /// Decodes the base64.
        /// </summary>
        /// <param name="encodedData">The encoded data.</param>
        /// <param name="outputFile">The output file.</param>
        /// <datetime>04/10/2012-10:59</datetime>
        public static void DecodeBase64(string encodedData, string outputFile)
        {
            byte[] decodeDataAsBytes = System.Convert.FromBase64String(encodedData);

            try
            {
                // Open file for writing
                System.IO.FileStream _FileStream = new System.IO.FileStream(outputFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);

                // Writes a block of bytes to this stream using data from a byte array.
                _FileStream.Write(decodeDataAsBytes, 0, decodeDataAsBytes.Length);

                // close file stream
                _FileStream.Close();
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
            }
        }

        /// <summary>
        /// Reads the encode data from file.
        /// </summary>
        /// <param name="inputFile">The input file.</param>
        /// <returns></returns>
        /// <datetime>04/10/2012-11:00</datetime>
        public static string readEncodeDataFromFile(string inputFile)
        {
            string encodeData = System.IO.File.ReadAllText(inputFile);
            return encodeData;
        }

    }
}
